package paper.db;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

public class PaperCache
{
    HashMap< String,Paper > papers;

    public PaperCache()
    {
        papers = new HashMap< String,Paper >();
    }

    Paper findKey( String key )
    {
        return papers.get( key );
    }

    void addPaper( Paper p)
    {
        if( papers.get( p.key ) == null )
            papers.put( p.key, p);
    }

    public void clearMarks()
    {
        for(Iterator<Paper> i = papers.values().iterator(); i.hasNext(); )
        {
            Paper p = i.next();
            p.mark = 0;
        }
    }

    public HashSet<Paper> getMarked(int threshold)
    {
        HashSet<Paper> result = new HashSet<Paper>();
        for(Iterator<Paper> i = papers.values().iterator(); i.hasNext(); )
        {
            Paper p = i.next();
            if( p.mark >= threshold )
                result.add( p );
        }
        return result;
    }

    public void dotCociteGraph()
    {
        clearMarks();
        System.out.println("digraph{");
        for(Iterator<Paper> i = papers.values().iterator(); i.hasNext(); )
        {
            Paper p = i.next();
            p.mark = 1;
            for(Iterator<Paper> j = p.cocites.iterator(); j.hasNext(); )
            {
                Paper pp = j.next();
                if(pp.mark==0)
                {
                    System.out.println("key" + p.key + " -> " + "key" + pp.key + " [dir=none]");
                    System.out.println("key" + p.key + " [label=\"" + p.bibTexKey + "\"];"); 
                    System.out.println("key" + pp.key + " [label=\"" + pp.bibTexKey + "\"];"); 
                }
            }
        }
        System.out.println("}");
    }
}
